﻿using Abp.Web.Models;
using Avis.OutBoundAPI.Contents.Dto;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using Microsoft.AspNetCore.Authorization;

namespace Avis.OutBoundAPI.Contents
{
    /// <summary>
    /// 内容CMS的应用服务
    /// </summary>
    public class ContentAppService : OutBoundAPIAppServiceBase, IContentAppService
    {
        /// <summary>
        /// 获取最新的特惠活动
        /// </summary>
        /// <returns></returns>
        [Authorize("force_hybrid")]
        public string GetLoginRequiredFunction()
        {
            return "success";
        }
        /// <summary>
        /// 获取首页的banner
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public IList<BannerOutput> GetIndexBanner()
        {
            // TODO 需要对内容CMS考虑不同的设备终端（PC和触屏）

            var httpClient = new HttpClient();
            var result = httpClient.GetStringAsync("http://139.224.131.42:58001/api/services/avis/content/GetIndexBanner");
            var ajaxResponse = JsonConvert.DeserializeObject<AjaxResponse<List<BannerOutput>>>(result.Result);
            if (!ajaxResponse.Success)
            {
                throw new Exception(ajaxResponse.Error?.Message ?? "Remote service throws exception!");
            }

            return ajaxResponse.Result;
        }

        /// <summary>
        /// 获取最新的特惠活动
        /// </summary>
        /// <returns></returns>
        public IList<PromotionListOutput> GetLasterPromotions()
        {
            var httpClient = new HttpClient();
            var result = httpClient.GetStringAsync("http://139.224.131.42:58001/api/services/avis/content/GetLasterPromotions");
            var ajaxResponse = JsonConvert.DeserializeObject<AjaxResponse<List<PromotionListOutput>>>(result.Result);
            if (!ajaxResponse.Success)
            {
                throw new Exception(ajaxResponse.Error?.Message ?? "Remote service throws exception!");
            }

            return ajaxResponse.Result;
        }

        /// <summary>
        /// 获取特惠详情
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public PromotionOutput Promotion(PromotionInput input)
        {
            var httpClient = new HttpClient();
            var result = httpClient.GetStringAsync("http://139.224.131.42:58001/api/services/avis/content/Promotion?input.id=3368");
            var ajaxResponse = JsonConvert.DeserializeObject<AjaxResponse<PromotionOutput>>(result.Result);
            if (!ajaxResponse.Success)
            {
                throw new Exception(ajaxResponse.Error?.Message ?? "Remote service throws exception!");
            }

            return ajaxResponse.Result;
        }
    }
}


